<?php
/**
 * 分类
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com
 * @link       http://www.ncphp.com
 * @author	   NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class ticket_categoryModel extends Model{

	public function __construct(){
		parent::__construct('ticket_category');
	}

    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
	public function getCategoryInfo($condition = array(), $field = '*')
    {
        return $this->field($field)->where($condition)->find();
    }

    /**
     * 插入一个分类
     * @param array $params
     * @return mixed
     */
    public function addCategory($params)
    {
        return $this->insert($params);
    }

    /**
     * 更新分类
     * @param array $condition
     * @param array $params
     * @return mixed
     */
    public function updateCategory($condition, $params)
    {
        return $this->where($condition)->update($params);
    }

    /**
     * 删除分类
     * @param integer $tc_id 分类ID
     * @return mixed
     */
    public function delCategory($tc_id)
    {
        $cateInfo = $this->getCategoryInfo(array('tc_id'=>$tc_id));
        //要下架的商品条件，分类删除了要下架
        $downCondition = $cateInfo['tc_pid'] == 0 ? array('cate1'=>$cateInfo['tc_id']) : array('cate2'=>$cateInfo['tc_id']);
        //这里执行下架操作

        //删除分类
        $delCondition = $cateInfo['tc_pid'] == 0 ? array(
            'tc_id' => $cateInfo['tc_id'],
            'tc_pid' => $cateInfo['tc_id'],
            '_op' => 'or'
        ) : array('tc_id'=>$tc_id);
        return $this->updateCategory($delCondition, array('is_del'=>1));
        //return $this->where($delCondition)->delete();
    }

    /**
     * 获取分类列表数组
     * @param array $condition
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public function getCategoryList($condition = array(), $field = '*', $order = 'tc_pid,tc_sort,tc_id'){
	    return $this->field($field)->where($condition)->order($order)->select();
    }

    /**
     * 获取分类树
     * @return array
     */
    public function getCategoryTree()
    {
        $list = $this->getCategoryList(array('is_del'=>0));
        $cateList = array();
        if (is_array($list) && !empty($list)) {
            foreach ($list as $k => $v) {
                if ($v['tc_pid'] == 0) {
                    $v['child'] = array();
                    foreach ($list as $kk => $vv) {
                        if ($vv['tc_pid'] == $v['tc_id']) {
                            $v['child'][] = $vv;
                        }
                    }
                    $cateList[] = $v;
                }
            }
        }
        return $cateList;
    }


}
?>